package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.SparePartDao;
import com.sugon.dao.SpareStorePosDao;
import com.sugon.dao.StoreHouseDao;
import com.sugon.entity.StoreHouseEntity;
import com.sugon.service.StoreHouseService;
import com.sugon.utils.ShiroUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 仓库Service实现类
 *
 * @author sugon
 * @date 2019-07-12 08:52:28
 */
@Service("storeHouseService")
public class StoreHouseServiceImpl implements StoreHouseService {

    @Resource
    private StoreHouseDao storeHouseDao;
    @Resource
    private SpareStorePosDao spareStorePosDao;
    @Resource
    private SparePartDao sparePartDao;

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public StoreHouseEntity queryObject(String id) {
        return storeHouseDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<StoreHouseEntity> queryList(Map<String, Object> map) {
        return storeHouseDao.queryList(map);
    }

    /**
     * allList
     *
     * @param map
     * @return
     */
    @Override
    public List<StoreHouseEntity> allList(Map<String, Object> map) {
        return storeHouseDao.allList(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return storeHouseDao.queryTotal(map);
    }

    /**
     * save
     *
     * @param storeHouse 实体
     * @return
     */
    @Override
    public int save(StoreHouseEntity storeHouse) {
        storeHouse.setId(RandomUtil.randomString(32));
        return storeHouseDao.save(storeHouse);
    }

    /**
     * update
     *
     * @param storeHouse 实体
     * @return
     */
    @Override
    public int update(StoreHouseEntity storeHouse) {
        storeHouse.setUpdateUser(ShiroUtils.getUserId());
        storeHouse.setUpdateDate(DateUtil.date());
        return storeHouseDao.update(storeHouse);
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delete(String id) {
        // 1、删除备件库位，逻辑删除
        spareStorePosDao.deleteByStoreId(id);
        // 2、重新计算下这个仓库中的备件库存数
        List<String> spareCodeList = spareStorePosDao.queryStoreHouseSpare(id);
        if (CollectionUtil.isNotEmpty(spareCodeList)) {
            for (String spareCode : spareCodeList) {
                sparePartDao.realtimeUpdateStock(spareCode);
            }
        }
        // 删除仓库
        return storeHouseDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return storeHouseDao.deleteBatch(ids);
    }

    /**
     * queryByWorkshop
     *
     * @param moduleType 仓库类别
     * @param workshop   车间ID
     * @return
     */
    @Override
    public StoreHouseEntity queryByWorkshop(Integer moduleType, String workshop) {
        Map<String, Object> param = new HashMap<>(1);
        param.put("workshop", workshop);
        param.put("moduleType", moduleType);
        List<StoreHouseEntity> storeHouseList = queryList(param);
        if (CollectionUtil.isNotEmpty(storeHouseList)) {
            return storeHouseList.get(0);
        }
        return null;
    }

    /**
     * listAll
     *
     * @return
     */
    @Override
    public Map<String, String> listAll() {
        List<StoreHouseEntity> storeHouseList = queryList(null);
        return storeHouseList.stream().collect(Collectors.toMap(StoreHouseEntity::getName, StoreHouseEntity::getId));
    }

}
